<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="/shop/include :: header"></head>
<body>
<div class="layui-fluid">
    <form id="form">
        <input type="hidden" name="level" th:value="${type}">
        <input th:if="${type != 1}" type="hidden" name="level_of_one" th:value="${id}">
        <input type="hidden" id="goodsCode" name="goods_code" th:value="${goodsCode}">
    </form>
    <table id="shop_goods_specifications-table" lay-filter="shop_goods_specifications-table"></table>
    <div style="float: right;display: grid">
        <span style="float: left;color: red" th:if="${type == '1'}"><i class="layui-icon layui-icon-about"></i>自定义规格确认后，请刷新列表</span>
        <span style="float: left;color: red" th:if="${type == '1'}"><i class="layui-icon layui-icon-about"></i>请谨慎刷新列表，刷新列表后所有商品属性需要重新上传</span>
    </div>
</div>
<!-- 工具栏 -->
<script type="text/html" id="toolbarShopGoodsSpecifications">
    <div class="layui-btn-container">
        <button th:if="${type == '1'}" shiro:hasPermission="shop:goods_specifications:add" class="layui-btn layui-btn-sm" lay-event="add"><i
                class="layui-icon layui-icon-add-circle"></i>添加
        </button>
        <button th:if="${type == '1'}" shiro:hasPermission="shop:goods_specifications:refresh" class="layui-btn layui-btn-sm" lay-event="refresh"><i
                class="layui-icon layui-icon-refresh-1"></i>刷新列表
        </button>
    </div>
</script>
<!-- 操作列 -->
<script type="text/html" id="shop_goods_specifications-state">
    <div class="layui-btn-group">
        <button shiro:hasPermission="shop:goods_specifications:edit" class="layui-btn layui-btn-sm" lay-event="edit"><i
                class="layui-icon layui-icon-edit"></i></button>
        <button shiro:hasPermission="shop:goods_specifications:remove" class="layui-btn layui-btn-sm" lay-event="del"><i
                class="layui-icon layui-icon-delete"></i></button>
    </div>
</script>
<script>

</script>
<div th:include="/shop/include::footer"></div>
<script>
    var conditions = $('#form').serialize();
    conditions = decodeURIComponent(conditions, true);
    layui.use(['table'], function () {
        var type = [[${type}]]
        var $ = layui.jquery;
        var table = layui.table;
        // 渲染表格
        var renderTable = function () {
            layer.load(2);
            table.render({
                elem: '#shop_goods_specifications-table',
                url: '/platform/goodsspecificationscustom/list',
                toolbar: '#toolbarShopGoodsSpecifications',
                page: true,
                id: 'shopGoodsSpecificationsReload',
                where: {"conditions": conditions},
                //limit : 1,
                cols: [  [   {
                    type: 'checkbox',
                    fixed: 'left'
                },
                    {
                        unresize: true,
                        sort: true,
                        width: 80,
                        align: 'center',
                        field: 'id',
                        title: 'ID',

                    },
                    {
                        align: 'center',
                        field: 'goodsCode',
                        title: '商品编码',

                    },
                    {
                        align: 'center',
                        field: 'specificationsName',
                        title: '规格名称',

                    },
                    {
                        align: 'center',
                        field: 'weight',
                        title: '权重',

                    },
                    {
                        align: 'center',
                        field: 'createTime',
                        title: '添加时间',

                    },
                    {
                        align: 'center',
                        field: 'updateTime',
                        title: '编辑时间',

                    },
                    {
                        align: 'center',
                        title: '查看下级',
                        hide: type == 1?false:true,
                        templet: function (d) {
                            var id = d.id;
                            return "<button type=\"button\" class=\"layui-btn layui-btn-xs\" onclick=readDown("+id+")>查看下级</button>";
                        }
                    },
                    {
                        align: 'center',
                        title: '启用状态',
                        templet: function (d) {
                            return d.enableState == 0 ? "已启用" : "未启用"
                        }
                    },
                    {
                        align: 'center',
                        title: '删除状态',
                        templet: function (d) {
                            return d.deleteFlag == 0 ? "未删除" : "已删除"
                        }
                    },
                    {
                        title: '操作',
                        toolbar: '#shop_goods_specifications-state',
                        align: 'center'
                    }   ]   ],
                done: function () {
                    layer.closeAll('loading');
                }
            });

        };
        renderTable();
        // 搜索
        var active = {
            reload: function () {
                //执行重载
                table.reload('shopGoodsSpecificationsReload', {
                    page: {
                        curr: 1
                        //重新从第 1 页开始
                    },
                    where: {}
                });
            }
        };

        $('#search').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //头工具栏事件
        table.on('toolbar(shop_goods_specifications-table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add'://增加
                    openAdd();
                    break;
                case 'refresh'://增加
                    var index =  layer.confirm('确定刷新商品列表，一旦操作不可撤销？', {
                        btn: ['确定', '取消']
                    }, function () {
                        layer.close(index)
                        layer.load(2);
                        //刷新列表
                        $.ajax({
                            url: '/platform/merchantsgoods/refreshGoodsListByGoodsCode?goodsCode=' + $("#goodsCode").val(),
                            type: "get",
                            async: false,
                            beforeSend: function (request) {
                                request.setRequestHeader(window.sessionStorage.getItem("tokenName"), window.sessionStorage.getItem("platform_token"));
                            },
                            success: function (result) {
                                if (result.status == "SUCCESS") {
                                    layer.msg(result.message, {
                                        offset: '15px'
                                        , icon: 1
                                        , time: 1000
                                    });
                                } else {
                                    layer.msg(result.message)
                                }
                            }
                        });
                        layer.closeAll('loading');
                        return false;
                    })
                    break;
                case 'batchRemove'://批量删除
                    var data = checkStatus.data;
                    if (data.length < 1) {
                        layer.msg('请至少选择一个');
                        break;
                    }
                    //批量删除
                    batchRemove(data);
            }
        });

        //监听工具条
        table.on('tool(shop_goods_specifications-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {//删除
                remove(data.id);
            } else if (layEvent === 'edit') {//修改
                edit(data.id);
            }
        });

        function remove(id) {
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消']
            }, function () {
                //删除
                ajaxPostLoad('/shop/goods_specifications/remove', {
                    id: id
                });
            })
        }

        function batchRemove(data) {
            layer.confirm("确认要删除选中的'" + data.length + "'条数据吗?", {
                btn: ['确定', '取消']
            }, function () {
                var ids = new Array();
                $.each(data, function (i, row) {
                    ids[i] = row['id'];
                });
                console.log("remove->ids:" + ids);
                //批量删除
                ajaxPostLoad('/shop/goods_specifications/batchRemove', {
                    ids: ids
                });
            });
        }

        function edit(id) {
            layer.open({
                type: 2,
                title: '修改',
                maxmin: true,
                shadeClose: false, // 点击遮罩关闭层
                area: ['100%', '100%'],
                content: "/platform/page/goods_specifications_custom/edit?id=" + id +"&url=/platform/goodsspecificationscustom/getGoodsSpecificationsCustomById" + "&key=shopGoodsSpecificationsCustom",
            });
        }

        function openAdd() {
            layer.open({
                type: 2,
                title: '增加',
                maxmin: true,
                shadeClose: false, // 点击遮罩关闭层
                area: ['100%', '100%'],
                content: '/platform/page/goods_specifications_custom/add?goodsCode='+$("#goodsCode").val() // iframe的url
            });
        }
    });

    function readDown(id) {
        layer.open({
            type: 2,
            title: '查看下级',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['100%', '100%'],
            content: '/platform/page/goods_specifications_custom/list?type=2'+'&id='+id + '&goodsCode=' + $("#goodsCode").val() // iframe的url
        });
    }
</script>
</body>
</html>